(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.companyNotifyEditController', companyNotifyEditController);
	companyNotifyEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig', '$filter'];
	function companyNotifyEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig, $filter) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("companyNotifyEditController");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.formName = "companyNotifyForm";
		
		ctrl.ok = function() {
			if($scope.companyNotifyForm) {
				$scope.companyNotifyForm.$setSubmitted();
				if(!$scope.companyNotifyForm.$valid) {
					dialogsModule.showErrorMessages($scope.companyNotifyForm);
					return;
				}
			}
			
			if (ctrl.companyNotify.eventsCodes == null || ctrl.companyNotify.eventsCodes.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'Notify Event is required!' ]
				});
				return false;
			}	
			
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.companyNotify) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			var ajaxService = ajaxServiceFactory.buildAjaxService({
				getLocaleOptions : {
					url : 'do/cda/dataList/locales',
					method : 'get'
				},
				getNotifyTypeOption: {
					url : 'do/cda/dataList/getNotifyTypeOptions',
					method : 'get'					
				},
				getYesNoOptions: {
					url : 'do/cda/dataList/yesno',
					method : 'get'					
				}
			});					
			if (ctrl.dialogsModule.params.notify) {
				ctrl.companyNotify = ctrl.dialogsModule.params.notify;				
			} else {
				ctrl.companyNotify = {};
				ctrl.companyNotify.docPwdFlag = "N";
			}
			if (ctrl.dialogsModule.params.notifyOld) {
				ctrl.companyNotifyOld = ctrl.dialogsModule.params.notifyOld;
			}
			ajaxService.getLocaleOptions().then(function(response) {
				ctrl.localeOptions = response;
			});
			//ctrl.localeOptions = {data: [{label: 'English (United States)', value: 'en_US'}, {label: 'Indonesian (United States)', value: 'in_US'}]}
			ajaxService.getYesNoOptions().then(function(response) {
				ctrl.docPwdFlagOptions = response;
			});
			ajaxService.getNotifyTypeOption().then(function(response) {
				ctrl.notifyTypeOptions = response;
			});
			//ctrl.notifyTypeOptions = {data: [{label: 'Email', value: 'E'}]}
			
			ctrl.notifyeventsOptions = {groups: []};
			var services = ctrl.dialogsModule.params.services;
			if (services) {
				authService.getServiceEvents().then(function(response) {
					if (response.data) {
						for (var i = 0; i < services.length; i++) {
							var service = services[i];
							if (service.companyServCmpts && service.companyServCmpts.length) {
								for (var j = 0; j < service.companyServCmpts.length; j++) {
									var companyServCmpnt = service.companyServCmpts[j];
									var servgrp;
									//for uneditable page.
									if (ctrl.dialogsModule.pageId == 'CDA_COMPANYNOTIFY_POPVIEW') {
										servgrp = {showHeadCheckbox: false, title: $filter('encode')(companyServCmpnt.componentCode)};
									} else {
										servgrp = {showHeadCheckbox: true, title: $filter('encode')(companyServCmpnt.componentCode)};
									}
									
									servgrp.data = [];
									ctrl.notifyeventsOptions.groups.push(servgrp);
									for (var m = 0; m < response.data.length; m++) {
										if (response.data[m].componentCode == $filter('encode')(companyServCmpnt.componentCode)){
											
											var option = {
													label: $filter('encode')(response.data[m].eventCode),
													value: response.data[m].eventCode
											};
											servgrp.data.push(option);
										}
										
									}
								}
							}
						}	
					}
				});
				
				
			}
		};

	}
})(angular);
